﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuanLyDeTaiKHCN.Obj_class;
using System.IO;
namespace QuanLyDeTaiKHCN.UserControls.Others
{
    public partial class QuanLyDeTaiSauNghiemThu : System.Web.UI.UserControl
    {
        public SecurityLib.Sec_AES _se = new SecurityLib.Sec_AES();
        _dbDataContext _db = new _dbDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string str = Request.QueryString["id"].Replace(" ", "+");
                string madetai = _se.Decrypt(str, "gold", 128);
                if (_db.tblHopHoiDongNghiemThus.Where(r => r.MaDeTai.Trim() == madetai).Count() == 0)
                {
                    pndanhsachdetaiduan.Visible = false;
                    lblThongBao.Visible = true;
                    lblThongBao.Text = "Đề tài này chưa được họp hội đồng nghiệm thu";
                }
                else
                {
                    BindGrid(madetai);
                    BindData();
                    inputID.Value = "";
                    pndanhsachdetaiduan.Visible = true;
                    lblThongBao.Visible = false;
                    lblThongBao.Text = "";
                }
            }
        }

        protected void BindData()
        {
            MemberShip_ASP.listDS.Clear();
            string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _se.Decrypt(str, "gold", 128);
            try
            {
                var item = _db.tblQuanLyDeTaiSauNghiemThus.Single(r => r.MaDeTai.Trim() == madetai);
                txtNgayNopBCNT.Text = string.Format("{0:dd/MM/yyyy}", item.NgayNhanBCNT);
                try { txtTomTacNoiDung.Text = item.TomTatKetQua.Trim(); }
                catch { }
                try { txtSoThanhLyHopDong.Text = item.SoTLHD.Trim(); }
                catch { }
                txtNgayThanhLyHopDong.Text = string.Format("{0:dd/MM/yyyy}",item.NgayTLHD);
                try { txtSoQuyetDinhConhNhanKetQua.Text = item.SoQDCNKQ.Trim(); }
                catch { }
                txtNgayQuyetDinhCNKQ.Text = string.Format("{0:dd/MM/yyyy}",item.NgayQDCNKQ);
                try { txtThongTinThem.Text = item.ThongTinThemCNKQ.Trim(); }
                catch { }
                //bind du lieu vao grid danh sach can bo tham gia
                string[] st1 = item.DanhSachCB.Split('@');
                string[] st;
                MemberShip_ASP.DSCanBo itemcanbo;
                for (int i = 0; i < st1.Length-1; i++)
                {
                    st = st1[i].Split('!');
                    itemcanbo = new MemberShip_ASP.DSCanBo();
                    itemcanbo.ID = int.Parse(st[0]);
                    itemcanbo.HoTen = st[1];
                    itemcanbo.DonVi = st[2];
                    itemcanbo.HocHam = st[3];
                    MemberShip_ASP.listDS.Add(itemcanbo);
                }
                gridds.DataSource = MemberShip_ASP.listDS;
                gridds.DataBind();
            }
            catch 
            {
                txtNgayNopBCNT.Text = "";
                txtTomTacNoiDung.Text = "";
                txtSoThanhLyHopDong.Text = "";
                txtNgayThanhLyHopDong.Text = "";
                txtSoQuyetDinhConhNhanKetQua.Text = "";
                txtNgayQuyetDinhCNKQ.Text = "";
                txtThongTinThem.Text = "";
                //bind du lieu vao grid danh sach can bo tham gia
                MemberShip_ASP.listDS.Clear();
                gridds.DataSource = MemberShip_ASP.listDS;
                gridds.DataBind();
            }
        }
        protected void LinkAddFile_Click(object sender, EventArgs e)
        {
            if (filebienban.HasFile)
            {
                string str = Request.QueryString["id"].Replace(" ", "+");
                string madetai = _se.Decrypt(str, "gold", 128);
                DateTime t = DateTime.Now;
                string filename = (t.Day < 10 ? "0" + t.Day.ToString() : t.Day.ToString()) + (t.Month < 10 ? "0" + t.Month.ToString() : t.Month.ToString()) + t.Year.ToString() + t.Hour.ToString() + t.Minute.ToString() + t.Second.ToString() + Path.GetExtension(filebienban.FileName);
                tblFile _tb = new tblFile();
                _tb.MaDeTaiDuAn = madetai;
                _tb.FileThuyetMinh = filename;
                _tb.GhiChuFileThuyetMinh = txtghichufilethuyetminh.Text;
                _tb.NgayNhanThuyetMinh = CatalogAccess.ConvertToDate(txtNgayNhan.Text);
                _tb.FileCuaBuoc = 13;
                try
                {
                    _db.tblFiles.InsertOnSubmit(_tb);
                    _db.SubmitChanges();
                    BindGrid(madetai);
                    filebienban.SaveAs(Server.MapPath(PageURL.local + "filehoso/" + filename));
                    txtghichufilethuyetminh.Text = "";
                }
                catch { }
            }
        }
        protected void BindGrid(string madetai)
        {
            var list = from p in _db.tblFiles.Where(r => r.MaDeTaiDuAn.Trim() == madetai && r.FileCuaBuoc == 13)
                       select new
                       {
                           ID = p.ID,
                           TrichYeu = p.GhiChuFileThuyetMinh.Trim(),
                           NgayLuu = string.Format("{0:dd/MM/yyyy}", p.NgayNhanThuyetMinh),
                           FileThuyetMinh = p.FileThuyetMinh.Trim(),
                           //sau khi up len host thi xai link1:
                           //Fileonline =domain_name + "\\" +directory+"\\"+ p.FileThuyetMinh.Trim()
                           //test xaif link2
                           //Fileonline = "localhost\\" +directory+"\\"+ p.FileThuyetMinh.Trim()
                           //demo link3:
                           Fileonline = "http://thongkekhcnbd.khcnbinhduong.gov.vn/userfiles/vanban/9475707965_493_296.pdf"

                       };
            grid.DataSource = list.ToList();
            grid.DataBind();
        }
        protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _se.Decrypt(str, "gold", 128);
            var item = _db.tblFiles.Single(r => r.ID == int.Parse(grid.DataKeys[e.RowIndex].Value.ToString()));
            try
            {
                _db.tblFiles.DeleteOnSubmit(item);
                _db.SubmitChanges();
                try
                {
                    string filename = ((Label)grid.Rows[e.RowIndex].FindControl("lblFileName")).Text;
                    System.IO.File.Delete(Server.MapPath(PageURL.local + "filehoso/" + filename));
                }
                catch { }
                BindGrid(madetai);
            }
            catch { }
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            if (inputID.Value == "")
            {
                MemberShip_ASP.DSCanBo item = new MemberShip_ASP.DSCanBo();
                try
                {
                    item.ID = MemberShip_ASP.listDS.Max(r => r.ID) + 1;
                }
                catch { item.ID = 1; }

                item.HoTen = txtHoTen.Text;
                item.DonVi = txtDonViCongTac.Text;
                item.HocHam = txtHocHamHocVi.Text;
                MemberShip_ASP.listDS.Add(item);
            }
            else
            {
                var item = MemberShip_ASP.listDS.Single(r => r.ID == int.Parse(inputID.Value));
                item.HoTen = txtHoTen.Text;
                item.DonVi = txtDonViCongTac.Text;
                item.HocHam = txtHocHamHocVi.Text;
            }
            gridds.DataSource = MemberShip_ASP.listDS;
            gridds.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            inputID.Value = "";
            txtHoTen.Text = "";
            txtDonViCongTac.Text = "";
            txtHocHamHocVi.Text = "";
        }

        protected void gridds_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var item = MemberShip_ASP.listDS.Single(r => r.ID == int.Parse(gridds.DataKeys[e.RowIndex].Value.ToString()));
            try
            {
                MemberShip_ASP.listDS.Remove(item);
                gridds.DataSource = MemberShip_ASP.listDS;
                gridds.DataBind();
            }
            catch { }
        }

        protected void gridds_RowEditing(object sender, GridViewEditEventArgs e)
        {
            inputID.Value = ((Label)gridds.Rows[e.NewEditIndex].FindControl("lblID")).Text;
            txtHoTen.Text = ((Label)gridds.Rows[e.NewEditIndex].FindControl("lblHoTen")).Text;
            txtDonViCongTac.Text = ((Label)gridds.Rows[e.NewEditIndex].FindControl("lblDonViCongTac")).Text;
            txtHocHamHocVi.Text = ((Label)gridds.Rows[e.NewEditIndex].FindControl("lblHocHamHocVi")).Text;

        }

        protected void LinkSavaAll_Click(object sender, EventArgs e)
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _se.Decrypt(str, "gold", 128);
            try//truong hop update
            {
                var _tb = _db.tblQuanLyDeTaiSauNghiemThus.Single(r => r.MaDeTai.Trim() == madetai);
                if (CatalogAccess.ConvertToDateBool(txtNgayNopBCNT.Text))
                    _tb.NgayNhanBCNT = CatalogAccess.ConvertToDate(txtNgayNopBCNT.Text);
                _tb.TomTatKetQua = txtTomTacNoiDung.Text;
                _tb.SoTLHD = txtSoThanhLyHopDong.Text;
                if (CatalogAccess.ConvertToDateBool(txtNgayThanhLyHopDong.Text))
                    _tb.NgayTLHD = CatalogAccess.ConvertToDate(txtNgayThanhLyHopDong.Text);
                _tb.SoQDCNKQ = txtSoQuyetDinhConhNhanKetQua.Text;
                if (CatalogAccess.ConvertToDateBool(txtNgayQuyetDinhCNKQ.Text))
                    _tb.NgayQDCNKQ = CatalogAccess.ConvertToDate(txtNgayQuyetDinhCNKQ.Text);
                _tb.ThongTinThemCNKQ = txtThongTinThem.Text;
                //tinh toan danh sach can bo tham gia
                string st = "";
                string st1 = "";
                foreach (var item in MemberShip_ASP.listDS)
                {
                    st1 = "";
                    st1 += item.ID.ToString() + "!";
                    st1 += item.HoTen.ToString() + "!";
                    st1 += item.DonVi.ToString() + "!";
                    st1 += item.HocHam.ToString();
                    st += st1 + "@";
                }
                _tb.DanhSachCB = st;
                try
                {
                    _db.SubmitChanges();
                    lblStatusAll.Text = "Đã lưu.";
                }
                catch { lblStatusAll.Text = "Không lưu được."; }
            }
            catch//truong hop them moi
            {
                tblQuanLyDeTaiSauNghiemThu _tb = new tblQuanLyDeTaiSauNghiemThu();
                _tb.MaDeTai = madetai;
                if (CatalogAccess.ConvertToDateBool(txtNgayNopBCNT.Text))
                    _tb.NgayNhanBCNT = CatalogAccess.ConvertToDate(txtNgayNopBCNT.Text);
                _tb.TomTatKetQua = txtTomTacNoiDung.Text;
                _tb.SoTLHD = txtSoThanhLyHopDong.Text;
                if (CatalogAccess.ConvertToDateBool(txtNgayThanhLyHopDong.Text))
                    _tb.NgayTLHD = CatalogAccess.ConvertToDate(txtNgayThanhLyHopDong.Text);
                _tb.SoQDCNKQ = txtSoQuyetDinhConhNhanKetQua.Text;
                if (CatalogAccess.ConvertToDateBool(txtNgayQuyetDinhCNKQ.Text))
                    _tb.NgayQDCNKQ = CatalogAccess.ConvertToDate(txtNgayQuyetDinhCNKQ.Text);
                _tb.ThongTinThemCNKQ = txtThongTinThem.Text;
                //tinh toan danh sach can bo tham gia
                string st = "";
                string st1 = "";
                foreach (var item in MemberShip_ASP.listDS)
                {
                    st1 = "";
                    st1 += item.ID.ToString()+"!";
                    st1 += item.HoTen.ToString() + "!";
                    st1 += item.DonVi.ToString() + "!";
                    st1 += item.HocHam.ToString();
                    st += st1 + "@";
                }
                _tb.DanhSachCB = st;
                try
                {
                    _db.tblQuanLyDeTaiSauNghiemThus.InsertOnSubmit(_tb);
                    _db.SubmitChanges();
                    lblStatusAll.Text = "Đã lưu.";
                }
                catch { lblStatusAll.Text = "Không lưu được."; }
            }
        }

        protected void btnexporttoword_Click(object sender, EventArgs e)
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _se.Decrypt(str, "gold", 128);
            var item = _db.tblQuanLyDeTaiSauNghiemThus.Where(r => r.MaDeTai.Trim() == madetai);
            if (item.Count() == 0 || item.First().DanhSachCB.Split('@').Length == 0)
            {
                lblStatusAll.Text = "Chưa có danh sách cán bộ tham gia đề tài";
            }
            else
            {
                Response.Redirect("Export/Mau6.aspx?id="+_se.Encrypt(madetai,"gold",128));
            }
        }
    }
}